/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookshop.managedBean;

import com.bookshop.entity.Book;
import com.bookshop.service.BookService;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Thibault
 */
public class SearchManagedBean {

    @EJB
    private BookService bookService;
    
    private List<Book> books = new ArrayList<Book>();
    private Book book = new Book();
        
    //private int catID = -1;

    private String simpleSearchLabel = "";
    
    private String titleLabel;
    private String publisherLabel;
    private String authorLabel;
    private String collectionLabel;
    private String serieLabel;
    private int typeId = -1;
    private int categoryId = -1;
    
    private String alertMessage;

    public List<Book> getBooks() {
        if (books.isEmpty()) {
            books = bookService.getAllBooks();
        }
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }
    
        public String getSimpleSearchLabel() {
        return simpleSearchLabel;
    }

    public void setSimpleSearchLabel(String search) {
        this.simpleSearchLabel = search;
    }
    
            /**
     * @return the titleLabel
     */
    public String getTitleLabel() {
        return titleLabel;
    }

    /**
     * @param titleLabel the titleLabel to set
     */
    public void setTitleLabel(String titleLabel) {
        this.titleLabel = titleLabel;
    }

    /**
     * @return the publisherLabel
     */
    public String getPublisherLabel() {
        return publisherLabel;
    }

    /**
     * @param publisherLabel the publisherLabel to set
     */
    public void setPublisherLabel(String publisherLabel) {
        this.publisherLabel = publisherLabel;
    }

    /**
     * @return the authorLabel
     */
    public String getAuthorLabel() {
        return authorLabel;
    }

    /**
     * @param authorLabel the authorLabel to set
     */
    public void setAuthorLabel(String authorLabel) {
        this.authorLabel = authorLabel;
    }

    /**
     * @return the collectionLabel
     */
    public String getCollectionLabel() {
        return collectionLabel;
    }

    /**
     * @param collectionLabel the collectionLabel to set
     */
    public void setCollectionLabel(String collectionLabel) {
        this.collectionLabel = collectionLabel;
    }

    /**
     * @return the serieLabel
     */
    public String getSerieLabel() {
        return serieLabel;
    }

    /**
     * @param serieLabel the serieLabel to set
     */
    public void setSerieLabel(String serieLabel) {
        this.serieLabel = serieLabel;
    }

    /**
     * @return the typeId
     */
    public int getTypeId() {
        return typeId;
    }

    /**
     * @param typeId the typeId to set
     */
    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }

    /**
     * @return the categoryId
     */
    public int getCategoryId() {
        return categoryId;
    }

    /**
     * @param categoryId the categoryId to set
     */
    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }
    
    /**
     * @return the alertMessage
     */
    public String getAlertMessage() {
        return alertMessage;
    }

    /**
     * @param alertMessage the alertMessage to set
     */
    public void setAlertMessage(String alertMessage) {
        this.alertMessage = alertMessage;
    }

    /**
     * Creates a new instance of SearchManagedBean
     */
    public SearchManagedBean() {
        
    }
    
    public String getAllBooks() {
        String returnValue = null;
        try {
            this.books.clear();
            this.books = bookService.getAllBooks();
            
            if (books.isEmpty()){
               returnValue = "toError";
            } else {
                returnValue = "toHome";
            }
            
        } catch (Exception ex) {
            Logger.getLogger(SearchManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return returnValue;
    }
    
    public String findBooksFromCategory() {
        String returnValue = null;
        try {
            this.books.clear();
            this.books = bookService.getAllBooksByCategoryId(
                    Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext()
                                                .getRequestParameterMap().get("catID")));
            if (this.books.isEmpty()) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"AVERTISSEMENT :", "Aucun livre n'appartient à la catégorie choisie."));
                returnValue = "toError";
            } else {
                returnValue = "toSearch";
            }
        } catch (Exception ex) {
            Logger.getLogger(SearchManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return returnValue;
    }
    
//    public void getBooksFromCategory(ActionEvent ae) {
//        try {
//            this.catID = Integer.parseInt(ae.getComponent().getAttributes().get("catID").toString());
//            this.books.clear();
//            this.books = bookService.getAllBooksByCategoryId(catID);
//            
//            if (books.isEmpty()){
//                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"AVERTISSEMENT :", "Aucun livre n'appartient à la catégorie choisie."));
//            }
//            
//        } catch (Exception ex) {
//            Logger.getLogger(SearchManagedBean.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }
    
    public String simpleSearch(){
        String returnValue = null;
        try {

            this.books.clear();
            this.books = bookService.simpleSearch(getSimpleSearchLabel().trim());
            
            if (books.isEmpty()){
               FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"AVERTISSEMENT :", "Aucun livre ne correspond à votre recherche."));
               returnValue = "toError";
            } else {
                returnValue = "toSearch";
            }
            
            simpleSearchLabel = "";
                                
        } catch (Exception ex) {
            Logger.getLogger(SearchManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return returnValue;
    }
    
    public String advancedSearch(){
        String returnValue = null;
        try {
//            this.categoryId = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("catID")));
//            this.typeId = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("typeID")));

            System.out.println("type = "+typeId+" | catégory = "+categoryId);
            this.books.clear();
            this.books = bookService.advancedSearch(getTitleLabel(), getPublisherLabel(), getCollectionLabel(), getAuthorLabel(), getSerieLabel(), getTypeId(), getCategoryId());
            
            if (books.isEmpty()){
               FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"AVERTISSEMENT :", "Aucun livre ne correspond à votre recherche."));
               returnValue = "toError";
            } else {
                returnValue = "toSearch";
            }
        
            titleLabel = "";
            publisherLabel = "";
            collectionLabel = "";
            serieLabel = "";
            authorLabel = "";
            typeId = -1;
            categoryId = -1;
            
        } catch (Exception ex) {
            Logger.getLogger(SearchManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return returnValue;
    }
}
